package com.lh.struts.filter;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import com.lh.jsp.urlparamEncoding.CharacterEncodingConstants;
import com.lh.log.log4j.LoggerImpl;
import com.lh.log.log4j.interfaces.Log;

public class RequestWrapperCharsetEncoding extends HttpServletRequestWrapper {
	private static Log logger = new LoggerImpl(HttpServletRequestWrapper.class);
	private String charset = "UTF-8";

	public RequestWrapperCharsetEncoding(HttpServletRequest request) {
		super(request);
	}
	 /** 
     * 获得被装饰对象的引用和采用的字符编码 
     * @param request 
     * @param charset 
     */  
    public RequestWrapperCharsetEncoding(HttpServletRequest request,  
            String charset) {  
        super(request);  
        this.charset = charset;  
    }  
    /** 
     * 实际上就是调用被包装的请求对象的getParameter方法获得参数，然后再进行编码转换 
     */  
    public String getParameter(String name) {  
        String value = super.getParameter(name);  
        value = value == null ? null : convertEncede(value.trim());//同时去掉参数的前后空格  
        return value;  
    }  
    public String convertEncede(String target) {  
    	logger.debug("编码转换之前：" + target);  
        try {  
            return new String(target.getBytes(CharacterEncodingConstants.IOS8859), charset);  
        } catch (UnsupportedEncodingException e) { 
        	logger.debug("有不支持的编码出现。");
            return target;  
        }  
    }  
  

}
